﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace Topro.Admin.Controllers
{

    using Topro.Application.Interface;

    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.IQS;
    using Topro.Entity.Response.IQS;
    using Topro.Util;

    /// <summary>
    /// IQS
    /// </summary>
    public class IQSController : BaseController
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IWebHostEnvironment _hostEnvironment;

        /// <summary>
        /// IQS接口
        /// </summary>
        private readonly IQSService _iqsService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="authService">IQS接口</param>
        public IQSController(IWebHostEnvironment hostEnvironment, IQSService iqsService)
        {
            _hostEnvironment = hostEnvironment;
            _iqsService = iqsService;
        }
        #endregion

        #region 列表
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AuthFilter(AuthCode.IQS0001)]
        public IActionResult Index()
        {
            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            return View();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<PermissionResponse></returns>
        [AuthFilter(AuthCode.IQS0001)]
        public JsonResult IQSQuery(IQSQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (DateUtil.IsDateTime(query.StartTime) && DateUtil.IsDateTime(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }
            var list = _iqsService.Pager(query);

            return Json(list);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">权限编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQS0003)]
        public IActionResult AddOrEdit(int? id)
        {
            var model = new IQSResponse();
            if (id.HasValue)
            {
                model = _iqsService.GetIqs(id.Value);
            }

            ViewBag.ShiftNum = EnumUtil.ToDictionary(typeof(IQSShiftNum));

            return View("Edit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQS0003)]
        public JsonResult SaveIQS(SaveIQSRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                #region Staff Num 员工号
                if (string.IsNullOrEmpty(request.StaffNum))
                {
                    throw new Exception("员工号不能为空.");
                }

                var staffNumReg = new Regex(@"^28(\d+)$");
                if (!staffNumReg.IsMatch(request.StaffNum))
                {
                    throw new Exception("员工号必须以28开头");
                }
                #endregion

                #region Shift Num 班次
                var shiftNum = new List<int>()
                {
                    (int)IQSShiftNum.A,
                    (int)IQSShiftNum.B,
                    (int)IQSShiftNum.C,
                    (int)IQSShiftNum.D,
                };

                if (!shiftNum.Contains(request.ShiftNum))
                {
                    throw new Exception("班次不能为空.");
                }
                #endregion

                #region Panel Quan 板数
                if (string.IsNullOrEmpty(request.PanelQuan))
                {
                    throw new Exception("板数介于不能为空.");
                }

                var panelQuanReg = new Regex(@"^[1-9]\d*$");
                if (!panelQuanReg.IsMatch(request.PanelQuan))
                {
                    throw new Exception("板数介于1~30整数.");
                }

                var panelQuan = Convert.ToInt32(request.PanelQuan);

                if (panelQuan <= 0 || panelQuan > 30)
                {
                    throw new Exception("板数介于1~30整数.");
                }
                #endregion

                request.Ip = Ip;
                request.UserId = UserId;

                response = _iqsService.SaveIQS(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 删除标志
        /// <summary>
        /// 删除标志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthFilter(AuthCode.IQS0001)]
        public JsonResult UpdateStatus(UpdateIQSRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            request.Ip = Ip;
            request.UserId = UserId;
            response = _iqsService.UpdateStatus(request);

            return Json(response);
        }
        #endregion

        #region 更新员工号
        /// <summary>
        /// 更新员工号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AuthFilter(AuthCode.IQS0001)]
        public JsonResult UpdateStaffNum(UpdateIQSRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            request.Ip = Ip;
            request.UserId = UserId;

            response = _iqsService.UpdateStaffNum(request);

            return Json(response);
        }
        #endregion

        #region IQS导出
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQS0002)]
        public IActionResult ExportExcels(IQSQuery query)
        {
            //获取数据
            var list = _iqsService.GetIqsExport(query);
            //文件名称
            var fileName = @$"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
             //路径
             var filePath = Path.Combine(@$"{_hostEnvironment.WebRootPath}", fileName);
            //文件对象
            FileInfo file = new FileInfo(filePath);
            //是否存在
            if(file.Exists)
            {
                file.Delete();
            }
            //指定EPPlus使用非商业证书
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (ExcelPackage package = new ExcelPackage(file))
            {
                //创建sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("IQS");
                //worksheet.Cells.LoadFromCollection(list, true);
                //头部
                worksheet.Cells[1, 1].Value = "厂号";
                worksheet.Cells[1, 2].Value = "员工号";
                worksheet.Cells[1, 3].Value = "班次";
                worksheet.Cells[1, 4].Value = "设备编码";
                worksheet.Cells[1, 5].Value = "批号";
                worksheet.Cells[1, 6].Value = " 料号";
                worksheet.Cells[1, 7].Value = "叠合次数号";
                worksheet.Cells[1, 8].Value = "材料类型";
                worksheet.Cells[1, 9].Value = "压合程式";
                worksheet.Cells[1, 10].Value = "铜箔编号";
                worksheet.Cells[1, 11].Value = "铜箔批号";
                worksheet.Cells[1, 12].Value = "铜箔有效期";
                worksheet.Cells[1, 13].Value = "半固化片种类数";
                worksheet.Cells[1, 14].Value = "半固化片数量";

                //料1_1
                worksheet.Cells[1, 15].Value = "零包OR整包";

                worksheet.Cells[1, 16].Value = "包号1_1";
                worksheet.Cells[1, 17].Value = "料号1_1";
                worksheet.Cells[1, 18].Value = "批号1_1";
                worksheet.Cells[1, 19].Value = "有效期1_1";
                worksheet.Cells[1, 20].Value = "延期材料1_1";
                //料1_2
                worksheet.Cells[1, 21].Value = "包号1_2";
                worksheet.Cells[1, 22].Value = "料号1_2";
                worksheet.Cells[1, 23].Value = "批号1_2";
                worksheet.Cells[1, 24].Value = "有效期1_2";
                worksheet.Cells[1, 25].Value = "延期材料1_2";

                //料2_1
                worksheet.Cells[1, 26].Value = "零包OR整包";

                worksheet.Cells[1, 27].Value = "包号2_1";
                worksheet.Cells[1, 28].Value = "料号2_1";
                worksheet.Cells[1, 29].Value = "批号2_1";
                worksheet.Cells[1, 30].Value = "有效期2_1";
                worksheet.Cells[1, 31].Value = "延期材料2_1";

                //料2_2
                worksheet.Cells[1, 32].Value = "包号2_2";
                worksheet.Cells[1, 33].Value = "料号2_2";
                worksheet.Cells[1, 34].Value = "批号2_2";
                worksheet.Cells[1, 35].Value = "有效期2_2";
                worksheet.Cells[1, 36].Value = "延期材料2_2";

                worksheet.Cells[1, 37].Value = "垫厚钢板数";
                worksheet.Cells[1, 38].Value = "满本结构";
                worksheet.Cells[1, 39].Value = "板数";
                worksheet.Cells[1, 40].Value = "含切片的板数";
                worksheet.Cells[1, 41].Value = "棕化停留时间";
                worksheet.Cells[1, 42].Value = "防滑质量";
                worksheet.Cells[1, 43].Value = "表面质量";
                worksheet.Cells[1, 44].Value = "14张牛皮纸数量";
                worksheet.Cells[1, 45].Value = "内层芯板质量";
                worksheet.Cells[1, 46].Value = "铜箔质量";
                worksheet.Cells[1, 47].Value = "异常钢板数量";
                worksheet.Cells[1, 48].Value = "备注";
                //行数
                int row = 2;

                foreach(var item in list)
                {
                    worksheet.Cells[row, 1].Value = item.PlntNum;
                    worksheet.Cells[row, 2].Value = item.StaffNum;
                    //
                    worksheet.Cells[row, 3].Value = item.ShiftNumString;
                    worksheet.Cells[row, 4].Value = item.EquipmentCode;
                    worksheet.Cells[row, 5].Value = item.LotBatch;
                    worksheet.Cells[row, 6].Value = item.PartNum;
                    worksheet.Cells[row, 7].Value = item.HeapNumber;
                    worksheet.Cells[row, 8].Value = item.MaterialType;
                    worksheet.Cells[row, 9].Value = item.PressProgram;
                    worksheet.Cells[row, 10].Value = item.CuFoilNum;
                    worksheet.Cells[row, 11].Value = item.CuFoilBatchNum;
                    worksheet.Cells[row, 12].Value = item.CuFoilExpire;
                    worksheet.Cells[row, 13].Value = item.PpTypeNumber;
                    worksheet.Cells[row, 14].Value = item.PpNumber;

                    //料1_1
                    //[零包OR整包]
                    //转换
                    worksheet.Cells[row, 15].Value = item.PostponedMaterialType1_1;

                    worksheet.Cells[row, 16].Value = item.PackageNum1_1;
                    worksheet.Cells[row, 17].Value = item.PpPcfPartNum1_1;
                    worksheet.Cells[row, 18].Value = item.PpPcfBatchNum1_1;
                    worksheet.Cells[row, 19].Value = item.PpPcfExpire1_1;
                    //转换
                    worksheet.Cells[row, 20].Value = item.PostponedMaterial1_1;
                    //料1_2
                    worksheet.Cells[row, 21].Value = item.PackageNum1_2;
                    worksheet.Cells[row, 22].Value = item.PpPcfPartNum1_2;
                    worksheet.Cells[row, 23].Value = item.PpPcfBatchNum1_2;
                    worksheet.Cells[row, 24].Value = item.PpPcfExpire1_2;
                    worksheet.Cells[row, 25].Value = item.PostponedMaterial1_2;

                    //料2_1
                    //[零包OR整包]
                    //转换
                    worksheet.Cells[row, 26].Value = item.PostponedMaterialType2_1;

                    worksheet.Cells[row, 27].Value = item.PackageNum2_1;
                    worksheet.Cells[row, 28].Value = item.PpPcfPartNum2_1;
                    worksheet.Cells[row, 29].Value = item.PpPcfBatchNum2_1;
                    worksheet.Cells[row, 30].Value = item.PpPcfExpire2_1;
                    //转换
                    worksheet.Cells[row, 31].Value = item.PostponedMaterial2_1;
                    //料2_2
                    worksheet.Cells[row, 32].Value = item.PackageNum2_2;
                    worksheet.Cells[row, 33].Value = item.PpPcfPartNum2_2;
                    worksheet.Cells[row, 34].Value = item.PpPcfBatchNum2_2;
                    worksheet.Cells[row, 35].Value = item.PpPcfExpire2_2;
                    //转换
                    worksheet.Cells[row, 36].Value = item.PostponedMaterial2_2;

                    //垫厚钢板数
                    worksheet.Cells[row, 37].Value = item.PlateNum;
                    //满本结构
                    worksheet.Cells[row, 38].Value = item.BookBuildupStructure;
                    //板数
                    worksheet.Cells[row, 39].Value = item.PanelQuan;
                    //含切片的板数
                    worksheet.Cells[row, 40].Value = item.SecionQuan;
                    //棕化停留时间
                    worksheet.Cells[row, 41].Value = item.AlphaBoardStayTime;

                    worksheet.Cells[row, 42].Value = item.StopperQualityString;
                    worksheet.Cells[row, 43].Value = item.PerpregSurfaceQualityString;
                    worksheet.Cells[row, 44].Value = item.PcsKraftQualityString;
                    worksheet.Cells[row, 45].Value = item.InnerCoreQualityString;
                    //Copper Foil Quality 铜箔质量
                    worksheet.Cells[row, 46].Value = item.CopperFoilQuality;
                    //异常钢板数量
                    worksheet.Cells[row, 47].Value = item.AbnormalSteelPlateQuantity;
                    worksheet.Cells[row, 48].Value = item.Remark;
                    row++;
                }
                package.Save();
            }

            return File(new FileStream(filePath, FileMode.Open), "application/octet-stream", fileName);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQS0002)]
        public IActionResult ExportExcel(IQSQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (DateUtil.IsDateTime(query.StartTime) && DateUtil.IsDateTime(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }
            //获取数据
            var list = _iqsService.GetIqsExport(query);
            //文件名称
            var fileName = @$"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            //路径
            var filePath = Path.Combine(@$"{_hostEnvironment.WebRootPath}", fileName);
            //文件对象
            FileInfo file = new FileInfo(filePath);
            //是否存在
            if (file.Exists)
            {
                file.Delete();
            }
            //指定EPPlus使用非商业证书
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (ExcelPackage package = new ExcelPackage(file))
            {
                //创建sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("IQS");
                //worksheet.Cells.LoadFromCollection(list, true);
                //头部
                //编号
                worksheet.Cells[1, 1].Value = "Event_ID";
                //材料类型
                worksheet.Cells[1, 2].Value = "Material_Type";
                //铜箔编号
                worksheet.Cells[1, 3].Value = "CuFoil_Num";
                //Package Num包号1_1
                worksheet.Cells[1, 4].Value = "Package_Num";
                //压合程式
                worksheet.Cells[1, 5].Value = "Press_Program";
                //记录状态
                worksheet.Cells[1, 6].Value = "Record_Status";
                //厂号
                worksheet.Cells[1, 7].Value = "Plant_Num";
                //Lot 批号
                worksheet.Cells[1, 8].Value = "Lot_Num";
                // 料号
                worksheet.Cells[1, 9].Value = "Part_Num";
                //叠合次数号
                worksheet.Cells[1, 10].Value = "Heap_Num";
                //设备编码
                worksheet.Cells[1, 11].Value = "Equipment_Code";
                //旋转排板
                worksheet.Cells[1, 12].Value = "Rotate_Layup";
                //批号1_1
                worksheet.Cells[1, 13].Value = "PP_RCF_Batch";
                //PP/PCF 有效期1_1
                worksheet.Cells[1, 14].Value = "Valid_Date";
                //满本结构
                worksheet.Cells[1, 15].Value = "Plate_Type";
                //垫厚钢板数
                worksheet.Cells[1, 16].Value = "Plate_Quantity";
                //板数
                worksheet.Cells[1, 17].Value = "Panel_Quantity";
                //含切片的板数
                worksheet.Cells[1, 18].Value = "Section_Panel_Size";
                //备注
                worksheet.Cells[1, 19].Value = "Remark";
                //员工号
                worksheet.Cells[1, 20].Value = "Staff_Num";
                //班次
                worksheet.Cells[1, 21].Value = "Shift_Num";
                //底板号码
                worksheet.Cells[1, 22].Value = "Tool_Num";
                //保存时间
                worksheet.Cells[1, 23].Value = "Date_Time";
                //扫流程卡时间(扫下条码的时间)
                worksheet.Cells[1, 24].Value = "Traveller_Date";
                //扫材料卡时间()
                worksheet.Cells[1, 25].Value = "Material_Date";
                //PP 半固化片数量
                worksheet.Cells[1, 26].Value = "PP_RCF_Quantity";
                //铜箔批号
                worksheet.Cells[1, 27].Value = "Cufoil_Batch";
                //铜箔有效期
                worksheet.Cells[1, 28].Value = "batch_date";

                //1-1
                //Package Num包号1_1
                worksheet.Cells[1, 29].Value = "Package_Num1_1";
                //PP/RCF 料号1_1
                worksheet.Cells[1, 30].Value = "PP_RCF_Part1_1";
                //PP/RCF 批号1_1
                worksheet.Cells[1, 31].Value = "PP_RCF_Batch1_1";
                //PP/RCF 有效期1_1
                worksheet.Cells[1, 32].Value = "Valid_Date1_1";

                //1-2
                //Package Num包号1_2
                worksheet.Cells[1, 33].Value = "Package_Num1_2";
                //PP/RCF 料号1_2
                worksheet.Cells[1, 34].Value = "PP_RCF_Part1_2";
                //PP/RCF 批号1_2
                worksheet.Cells[1, 35].Value = "PP_RCF_Batch1_2";
                //PP/RCF 有效期1_2
                worksheet.Cells[1, 36].Value = "Valid_Date1_2";

                //2-1
                //Package Num包号2_1
                worksheet.Cells[1, 37].Value = "Package_Num2_1";
                //PP/RCF 料号2_1
                worksheet.Cells[1, 38].Value = "PP_RCF_Part2_1";
                //PP/RCF 批号2_1
                worksheet.Cells[1, 39].Value = "PP_RCF_Batch2_1";
                //PP/RCF 有效期2_1
                worksheet.Cells[1, 40].Value = "Valid_Date2_1";

                //2-2
                //Package Num包号2_2
                worksheet.Cells[1, 41].Value = "Package_Num2_2";
                //PP/RCF 料号2_2
                worksheet.Cells[1, 42].Value = "PP_RCF_Part2_2";
                //PP/RCF 批号2_2
                worksheet.Cells[1, 43].Value = "PP_RCF_Batch2_2";
                //PP/RCF 有效期2_2
                worksheet.Cells[1, 44].Value = "Valid_Date2_2";


                //棕化停留时间
                worksheet.Cells[1, 45].Value = "Alpha_Board_Stay_Time";
                //防滑质量
                worksheet.Cells[1, 46].Value = "Stopper_Quality";
                //表面质量
                worksheet.Cells[1, 47].Value = "Prepreg_Surface_Quality";
                //14张牛皮纸数量
                worksheet.Cells[1, 48].Value = "Pcs_14_Kraft_Quantity";
                //内层芯板质量
                worksheet.Cells[1, 49].Value = "Inner_Core_Quality";
                //铜箔质量
                worksheet.Cells[1, 50].Value = "Copper_Foil_Quality";
                //钢板质量
                worksheet.Cells[1, 51].Value = "Steel_plate_Quality";
                //异常钢板数量
                worksheet.Cells[1, 52].Value = "Abnormal_Steel_Plate_Quantity";
                //Book数
                worksheet.Cells[1, 53].Value = "BookNum";
                //拆板设备
                worksheet.Cells[1, 54].Value = "Break of machine";
                //拆板时间
                worksheet.Cells[1, 55].Value = "Break off time";
                //拆板人
                worksheet.Cells[1, 56].Value = "Break off OP";

                //行数
                int row = 2;

                foreach (var item in list)
                {
                    //编号
                    worksheet.Cells[row, 1].Value = item.Id;
                    //材料类型
                    worksheet.Cells[row, 2].Value = item.MaterialType;
                    //铜箔编号
                    worksheet.Cells[row, 3].Value = item.CuFoilNum;
                    //Package Num包号1_1
                    worksheet.Cells[row, 4].Value = item.PackageNum1_1;
                    //压合程式
                    worksheet.Cells[row, 5].Value = item.PressProgramFull;
                    //记录状态
                    worksheet.Cells[row, 6].Value = item.DeleteStatus == 1 ? 0 : 1;
                    //厂号
                    worksheet.Cells[row, 7].Value = item.PlntNum;
                    //Lot 批号
                    worksheet.Cells[row, 8].Value = item.LotBatch;
                    // 料号
                    worksheet.Cells[row, 9].Value = item.PartNum;
                    //叠合次数号
                    worksheet.Cells[row, 10].Value = item.HeapNumber;
                    //设备编码
                    worksheet.Cells[row, 11].Value = item.EquipmentCode;
                    //旋转排板
                    worksheet.Cells[row, 12].Value = "YES";

                    //批号1_1
                    worksheet.Cells[row, 13].Value = item.PpPcfBatchNum1_1;
                    //PP/PCF 有效期1_1
                    worksheet.Cells[row, 14].Value = item.PpPcfExpire1_1;
                    //满本结构
                    worksheet.Cells[row, 15].Value = item.BookBuildupStructure;
                    //垫厚钢板数
                    worksheet.Cells[row, 16].Value = item.PlateNum;
                    //板数
                    worksheet.Cells[row, 17].Value = item.PanelQuan;
                    //含切片的板数
                    worksheet.Cells[row, 18].Value = item.SecionQuan;
                    //备注
                    worksheet.Cells[row, 19].Value = item.Remark;
                    //员工号
                    worksheet.Cells[row, 20].Value = item.StaffNum;
                    //班次
                    worksheet.Cells[row, 21].Value = (ShiftNum)item.ShiftNum;
                    //底板号码
                    worksheet.Cells[row, 22].Value = string.IsNullOrEmpty(item.ContainerCode) ? "null" : item.ContainerCode;
                    //保存时间
                    worksheet.Cells[row, 23].Value = item.CreatedDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    //流程卡时间
                    var travellerDate = "";
                    if(item.TravellerDate.HasValue)
                    {
                        travellerDate = item.TravellerDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    worksheet.Cells[row, 24].Value = travellerDate;
                    //扫材料卡时间
                    var materialDate = "";
                    try
                    {
                        if(item.MaterialDate.HasValue)
                        {
                            materialDate = item.MaterialDate.Value.ToString("yyy-MM-dd HH:mm:ss");
                        }
                        //if (!string.IsNullOrEmpty(item.PackageNum1_1))
                        //{
                        //    var dt = DateTime.ParseExact(item.PackageNum1_1.Replace("-", ""), "yyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                        //    //转换
                        //    materialDate = dt.ToString("yyy-MM-dd HH:mm:ss");
                        //}
                    }
                    catch(Exception ex)
                    {
                    }
                    worksheet.Cells[row, 25].Value = materialDate;
                    //PP 半固化片数量
                    worksheet.Cells[row, 26].Value = item.PpNumber;
                    //铜箔批号
                    worksheet.Cells[row, 27].Value = item.CuFoilBatchNum;
                    //铜箔有效期
                    worksheet.Cells[row, 28].Value = item.CuFoilExpire;

                    //1-1
                    //Package Num包号1_1
                    worksheet.Cells[row, 29].Value = item.PackageNum1_1;
                    //PP/RCF 料号1_1
                    worksheet.Cells[row, 30].Value = item.PpPcfPartNum1_1;
                    //PP/RCF 批号1_1
                    worksheet.Cells[row, 31].Value = item.PpPcfBatchNum1_1;
                    //PP/RCF 有效期1_1
                    worksheet.Cells[row, 32].Value = item.PpPcfExpire1_1;

                    //1-2
                    //Package Num包号1_2
                    worksheet.Cells[row, 33].Value = item.PackageNum1_2;
                    //PP/RCF 料号1_2
                    worksheet.Cells[row, 34].Value = item.PpPcfPartNum1_2;
                    //PP/RCF 批号1_2
                    worksheet.Cells[row, 35].Value = item.PpPcfBatchNum1_2;
                    //PP/RCF 有效期1_2
                    worksheet.Cells[row, 36].Value = item.PpPcfExpire1_2;

                    //2-1
                    //Package Num包号2_1
                    worksheet.Cells[row, 37].Value = item.PackageNum2_1;
                    //PP/RCF 料号2_1
                    worksheet.Cells[row, 38].Value = item.PpPcfPartNum2_1;
                    //PP/RCF 批号2_1
                    worksheet.Cells[row, 39].Value = item.PpPcfBatchNum2_1;
                    //PP/RCF 有效期2_1
                    worksheet.Cells[row, 40].Value = item.PpPcfExpire2_1;

                    //2-2
                    //Package Num包号2_2
                    worksheet.Cells[row, 41].Value = item.PackageNum2_2;
                    //PP/RCF 料号2_1
                    worksheet.Cells[row, 42].Value = item.PpPcfPartNum2_2;
                    //PP/RCF 批号2_1
                    worksheet.Cells[row, 43].Value = item.PpPcfBatchNum2_2;
                    //PP/RCF 有效期2_1
                    worksheet.Cells[row, 44].Value = item.PpPcfExpire2_2;


                    //棕化停留时间
                    worksheet.Cells[row, 45].Value = item.AlphaBoardStayTime;
                    //防滑质量
                    worksheet.Cells[row, 46].Value = item.StopperQualityString;
                    //表面质量
                    worksheet.Cells[row, 47].Value = item.PerpregSurfaceQualityString;
                    //14张牛皮纸数量
                    worksheet.Cells[row, 48].Value = item.PcsKraftQualityString;
                    //内层芯板质量
                    worksheet.Cells[row, 49].Value = item.InnerCoreQualityString;
                    //铜箔质量
                    worksheet.Cells[row, 50].Value = item.CopperFoilQualityString;
                    //钢板质量
                    var steelPlateQuality = "";

                    if(item.SteelPlateQuality == 10)
                    {
                        steelPlateQuality = "OK";
                    }
                    else if(item.SteelPlateQuality == 20)
                    {
                        steelPlateQuality = "NG 钢板划伤、下陷";
                    }
                    else if (item.SteelPlateQuality == 30)
                    {
                        steelPlateQuality = "NG 钢板异物残留";
                    }
                    else if (item.SteelPlateQuality == 40)
                    {
                        steelPlateQuality = "NG 钢板板翘";
                    }
                    worksheet.Cells[row, 51].Value = steelPlateQuality;
                    //异常钢板数量
                    worksheet.Cells[row, 52].Value = item.AbnormalSteelPlateQuantity;
                    //Book数
                    worksheet.Cells[row, 53].Value = item.BookNum;
                    //拆板设备
                    worksheet.Cells[row, 54].Value = item.WatchmanMachineCode;
                    //拆板时间
                    worksheet.Cells[row, 55].Value = item.WatchmanDate;
                    //拆板人
                    worksheet.Cells[row, 56].Value = item.WatchmanBy;
                    row++;
                }
                package.Save();
            }

            return File(new FileStream(filePath, FileMode.Open), "application/octet-stream", fileName);
        }
        #endregion

        #region IQS操作
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AuthFilter(AuthCode.IQSOpt0001)]
        public IActionResult OptIndex()
        {
            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            return View("OptIndex");
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<PermissionResponse></returns>
        [AuthFilter(AuthCode.IQSOpt0001)]
        public JsonResult OptQuery(IQSQuery query)
        {
            if (!query.Id.HasValue)
            {
                query.Id = 0;
            }

            query.UserPermissions = UserPermissions;

            var list = _iqsService.GetOptIqsPager(query);

            return Json(list);
        }

        /// <summary>
        /// 管理员新增、编辑
        /// </summary>
        /// <param name="id">权限编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQSOpt0002)]
        public IActionResult AdminAddOrEdit(int? id)
        {
            var model = new IQSResponse();
            if (id.HasValue)
            {
                model = _iqsService.GetIqs(id.Value);
            }

            ViewBag.ShiftNum = EnumUtil.ToDictionary(typeof(IQSShiftNum));

            return View(model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.IQS0003)]
        public JsonResult AdminSaveIQS(SaveIQSRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //机台编号
                if (string.IsNullOrEmpty(request.MachineCode))
                {
                    throw new Exception("机台编号不能为空.");
                }
                //设备编码
                if (string.IsNullOrEmpty(request.EquipmentCode))
                {
                    throw new Exception("设备编码不能为空.");
                }
                //厂号
                if (request.PlntNum<=0)
                {
                    throw new Exception("厂号不能为空.");
                }
                //批号
                if (string.IsNullOrEmpty(request.LotBatch))
                {
                    throw new Exception("批号不能为空.");
                }
                //料号
                if (string.IsNullOrEmpty(request.PartNum))
                {
                    throw new Exception("料号不能为空.");
                }
                //叠合次数号
                if (string.IsNullOrEmpty(request.HeapNumber))
                {
                    throw new Exception("叠合次数号不能为空.");
                }
                //流程卡时间
                if (!DateUtil.IsDateTime(request.TravellerDate))
                {
                    throw new Exception("流程卡时间错误.");
                }
                //压合程式
                if (string.IsNullOrEmpty(request.PressProgram))
                {
                    throw new Exception("压合程式不能为空.");
                }
                //材料时间
                if (!DateUtil.IsDateTime(request.MaterialDate))
                {
                    throw new Exception("材料时间时间错误.");
                }
                //创建时间
                if (!DateUtil.IsDateTime(request.CreatedDate))
                {
                    throw new Exception("创建时间错误.");
                }
                request.Ip = Ip;
                request.UserId = UserId;

                response = _iqsService.AdminSaveIQS(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion
    }
}
